<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 小虎哥 <1105415366@qq.com>
 * Date: 2018-4-3
 */

namespace think\template\taglib\eyou;

use think\Db;

/**
 * 优惠券 - 领券中心列表
 */
class TagCouponlist extends Base
{
    
    //初始化
    protected function _initialize()
    {
        parent::_initialize();
        $this->users_id = session('users_id');
        $this->users_id = !empty($this->users_id) ? $this->users_id : 0;
    }

    /**
     * 获取分页列表
     */
    public function getCouponlist($limit = '',$orderby = '', $ordermode = '')
    {
        $where['end_date'] = ['>', getTime()];
        $where['status'] = 1;
        $where['is_del'] = 0;

        $order = $orderby.' '.$ordermode;

        /*列表分页URL问号的查询部分*/
        $get_arr = input('get.');
        foreach ($get_arr as $key => $val) {
            if (empty($val) || stristr($key, '/')) {
                unset($get_arr[$key]);
            }
        }
        $param_arr = input('param.');
        foreach ($param_arr as $key => $val) {
            if (empty($val) || stristr($key, '/')) {
                unset($param_arr[$key]);
            }
        }
        $seo_pseudo = tpCache('seo.seo_pseudo');
        if ($seo_pseudo == 1) { // 动态URL模式
            $query_get = $get_arr;
        } elseif ($seo_pseudo == 3) { // 伪静态URL模式
            $uiset = input('param.uiset/s', 'off');
            $uiset = trim($uiset, '/');
            if ($uiset == 'on') {// 装修模式下的分类URL
                $query_get = $get_arr;
            } else {
                /*正常模式下的分页链接*/
                /*                        $diff_arr = array_diff_key($param_arr, $get_arr);
                                        if (empty($diff_arr)) {
                                            $query_get = array();
                                        } else {
                                            $query_get = $get_arr;
                                        }*/
                /*--end*/
                $query_get = array(
                    'typeid_tmp'   => $this->tid,
                );
            }
        } elseif ($seo_pseudo == 2) {
            $query_get = $get_arr;
        }
        /*--end*/

        $paginate_type = config('paginate.type');
        if (isMobile()) {
            $paginate_type = 'mobile';
        }
        $paginate = array(
            'type'  => $paginate_type,
            'var_page' => config('paginate.var_page'),
            'query' => $query_get,
        );
        $pages = Db::name('shop_coupon')
            ->where($where)
            ->orderRaw($order)
            ->paginate($limit, false, $paginate);
        $list = $pages->toArray();
        $version   = getCmsVersion();

        foreach ($list['data'] as $k => $v) {
            $url = url('api/Ajax/eyou_get_coupon',['coupon_id'=>$v['coupon_id']]);
            $v['onclick'] = " href=\"javascript:void(0);\" data-url='{$url}' onClick=\"eyou_tag_get_coupon(this);\"";
            $v['get'] = 0;
            $v['sale_percent'] = ceil(($v['coupon_geted']/($v['coupon_stock'] + $v['coupon_geted']))*100);
            if (($k+1) == count($list['data'])){
                $srcurl = get_absolute_url("{$this->root_dir}/public/static/common/js/tag_couponlist.js?t={$version}");
                $v['hidden'] = <<<EOF
<script language="javascript" type="text/javascript" src="{$srcurl}"></script>
EOF;
            }
            $list['data'][$k] = $v;
        }

        if (!empty($this->users_id) && !empty($list['data'])) {
            $coupon_ids = get_arr_column($list['data'], 'coupon_id');
            //我领取的优惠券
            $my_coupon = Db::name('shop_coupon_use')->where('coupon_id', 'in', $coupon_ids)->where('use_status',0)->where('users_id', $this->users_id)->group('coupon_id')->getAllWithIndex('coupon_id');
            if (!empty($my_coupon)) {
                foreach ($list['data'] as $k => $v) {
                    if (!empty($my_coupon[$v['coupon_id']])){
                        $list['data'][$k]['get'] = 1;
                        $url = url('user/Coupon/index');
                        $list['data'][$k]['onclick'] = " href='{$url}' ";
                    }
                }
            }
        }

        $result['pages'] = $pages; // 分页显示输出
        $result['list'] = $list['data']; // 赋值数据集

        return $result;
    }
}